package facade;

import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import DAO.*;
import br.*;

public class RestauranteFacade {
	
	private static EntityManagerFactory factory = Persistence.createEntityManagerFactory("restaurante");
	
	// ---------------------------------------------------------------------------------------
						//	CLIENTE
	// ---------------------------------------------------------------------------------------
	
	// INSERIR CLIENTE
	public static void inserirCliente(Cliente cliente){
		EntityManager manager = factory.createEntityManager();
		
		ClienteDAO clienteDAO = new ClienteDAO(manager);
		
		clienteDAO.inserir(cliente);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// ATUALIZAR CLIENTE
	public static void atualizarCliente(Cliente cliente){
		EntityManager manager = factory.createEntityManager();
		
		ClienteDAO clienteDao = new ClienteDAO(manager);
		
		clienteDao.atualizar(cliente);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// REMOVER CLIENTE
	public static void removerCliente(Cliente cliente) throws SQLException{
		EntityManager manager = factory.createEntityManager();
		
		ClienteDAO clienteDAO = new ClienteDAO(manager);
		
		clienteDAO.remover(cliente);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// LISTAR CLIENTE
	public static List<Cliente> listarClientes(){
		EntityManager manager = factory.createEntityManager();
		
		ClienteDAO clienteDAO = new ClienteDAO(manager);
		
		return clienteDAO.listarTodos();
		
	}
	
	// ---------------------------------------------------------------------------------------
					//	CARDAPIO
	// ---------------------------------------------------------------------------------------
	
	// INSERIR CARDAPIO
	public static void inserirCardapio(Cardapio cardapio){
		EntityManager manager = factory.createEntityManager();
		
		CardapioDAO cardapioDAO = new CardapioDAO(manager);
		
		cardapioDAO.inserir(cardapio);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// ATUALIZAR CARDAPIO
	public static void atualizarCardapio(Cardapio cardapio){
		EntityManager manager = factory.createEntityManager();
		
		CardapioDAO cardapioDao = new CardapioDAO(manager);
		
		cardapioDao.atualizar(cardapio);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	
	//LISTAR CARDAPIO POR NOME
		public static List<Cardapio> listarCardapioPorNome(String nome){
			EntityManager manager = factory.createEntityManager();
			CardapioDAO cardapioDao = new CardapioDAO(manager);
			return cardapioDao.listarPorAtributo("nome", nome);
		}
	
	// REMOVER CARDAPIO
	public static void removerCardapio(Cardapio cardapio) throws SQLException{
		EntityManager manager = factory.createEntityManager();
		
		CardapioDAO cardapioDAO = new CardapioDAO(manager);
		
		cardapioDAO.remover(cardapio);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// LISTAR CARDAPIO
	public static List<Cardapio> listarCardapio(){
		EntityManager manager = factory.createEntityManager();
		
		CardapioDAO cardapioDAO = new CardapioDAO(manager);
		
		return cardapioDAO.listarTodos();
		
	}
	
	// ---------------------------------------------------------------------------------------
					//	CATEGORIA
	// ---------------------------------------------------------------------------------------
	
	// INSERIR CATEGORIA
	public static void inserirCategoria(Categoria categoria){
		EntityManager manager = factory.createEntityManager();
		
		CategoriaDAO categoriaDAO = new CategoriaDAO(manager);
		
		categoriaDAO.inserir(categoria);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// ATUALIZAR CATEGORIA
	public static void atualizarCategoria(Categoria categoria){
		EntityManager manager = factory.createEntityManager();
		
		CategoriaDAO categoriaDao = new CategoriaDAO(manager);
		
		categoriaDao.atualizar(categoria);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	//LISTAR CATEGORIA POR NOME
	public static List<Categoria> listarCategoriaPorNome(String nome){
		EntityManager manager = factory.createEntityManager();
		CategoriaDAO categoriaDao = new CategoriaDAO(manager);
		return categoriaDao.listarPorAtributo("nome", nome);
	}
	
	// REMOVER CATEGORIA
	public static void removerCategoria(Categoria categoria) throws SQLException{
		EntityManager manager = factory.createEntityManager();
		
		CategoriaDAO categoriaDAO = new CategoriaDAO(manager);
		categoria = categoriaDAO.buscarPorId(categoria.getId());
		categoriaDAO.remover(categoria);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// LISTAR CATEGORIA
	public static List<Categoria> listarCategoria(){
		EntityManager manager = factory.createEntityManager();
		
		CategoriaDAO categoriaDAO = new CategoriaDAO(manager);
		
		return categoriaDAO.listarTodos();
		
	}
	
	//BUSCA CATEGORIA
	public static Categoria buscarCategoria(Long id){
		EntityManager manager = factory.createEntityManager();
		CategoriaDAO categoriaDao = new CategoriaDAO(manager);
		return categoriaDao.buscarPorId(id);
	}
	
	// ---------------------------------------------------------------------------------------
				//	DELIVERY
	// ---------------------------------------------------------------------------------------
	
	// INSERIR DELIVERY
	public static void inserirDelivery(Delivery delivery){
		EntityManager manager = factory.createEntityManager();
		
		DeliveryDAO deliveryDAO = new DeliveryDAO(manager);
		
		deliveryDAO.inserir(delivery);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// ATUALIZAR DELIVERY
	public static void atualizarDelivery(Delivery delivery){
		EntityManager manager = factory.createEntityManager();
		
		DeliveryDAO deliveryDao = new DeliveryDAO(manager);
		
		deliveryDao.atualizar(delivery);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// REMOVER DELIVERY
	public static void removerDelivery(Delivery delivery) throws SQLException{
		EntityManager manager = factory.createEntityManager();
		
		DeliveryDAO deliveryDAO = new DeliveryDAO(manager);
		
		deliveryDAO.remover(delivery);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	// LISTAR DELIVERY
	public static List<Delivery> listarDelivery(){
		EntityManager manager = factory.createEntityManager();
		
		DeliveryDAO deliveryDAO = new DeliveryDAO(manager);
		
		return deliveryDAO.listarTodos();
		
	}
	
	// ---------------------------------------------------------------------------------------
				//	ENDERECO
	// ---------------------------------------------------------------------------------------

	// INSERIR ENDERECO
	public static void inserirEndereco(Endereco endereco) {
		EntityManager manager = factory.createEntityManager();

		EnderecoDAO enderecoDAO = new EnderecoDAO(manager);

		enderecoDAO.inserir(endereco);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// ATUALIZAR ENDERECO
	public static void atualizarEndereco(Endereco endereco) {
		EntityManager manager = factory.createEntityManager();

		EnderecoDAO enderecoDao = new EnderecoDAO(manager);

		enderecoDao.atualizar(endereco);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// REMOVER ENDERECO
	public static void removerEndereco(Endereco endereco) throws SQLException {
		EntityManager manager = factory.createEntityManager();

		EnderecoDAO enderecoDAO = new EnderecoDAO(manager);

		enderecoDAO.remover(endereco);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// LISTAR ENDERECO
	public static List<Endereco> listarEndereco() {
		EntityManager manager = factory.createEntityManager();

		EnderecoDAO enderecoDAO = new EnderecoDAO(manager);

		return enderecoDAO.listarTodos();

	}

	// ---------------------------------------------------------------------------------------
				// FUNCIONARIO
	// ---------------------------------------------------------------------------------------

	// INSERIR FUNCIONARIO
	public static void inserirFuncionario(Funcionario funcionario) {
		EntityManager manager = factory.createEntityManager();

		FuncionarioDAO funcionarioDAO = new FuncionarioDAO(manager);

		funcionarioDAO.inserir(funcionario);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	
	public static List<Funcionario> listarFuncionarioPorNome(String nome){
		EntityManager manager = factory.createEntityManager();
		FuncionarioDAO funcionarioDao = new FuncionarioDAO(manager);
		return funcionarioDao.listarPorAtributo("nome", nome);
	}
	
	
	// ATUALIZAR FUNCIONARIO
	public static void atualizarFuncionario(Funcionario funcionario) {
		EntityManager manager = factory.createEntityManager();

		FuncionarioDAO funcionarioDao = new FuncionarioDAO(manager);

		funcionarioDao.atualizar(funcionario);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	

	//BUSCA FUNCIONARIO
		public static Funcionario buscarFuncionario(Long id){
			EntityManager manager = factory.createEntityManager();
			FuncionarioDAO funcionarioDao = new FuncionarioDAO(manager);
			return funcionarioDao.buscarPorId(id);
		}
	
	
	// REMOVER FUNCIONARIO
	public static void removerFuncionario(Funcionario funcionario) throws SQLException {
		EntityManager manager = factory.createEntityManager();

		FuncionarioDAO funcionarioDAO = new FuncionarioDAO(manager);

		funcionarioDAO.remover(funcionario);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// LISTAR FUNCIONARIO
	public static List<Funcionario> listarFuncionario() {
		EntityManager manager = factory.createEntityManager();

		FuncionarioDAO funcionarioDAO = new FuncionarioDAO(manager);

		return funcionarioDAO.listarTodos();

	}

	// ---------------------------------------------------------------------------------------
				// GERENTE
	// ---------------------------------------------------------------------------------------

	// INSERIR GERENTE
	public static void inserirGerente(Gerente gerente) {
		EntityManager manager = factory.createEntityManager();

		GerenteDAO gerenteDAO = new GerenteDAO(manager);

		gerenteDAO.inserir(gerente);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// ATUALIZAR GERENTE
	public static void atualizarGerente(Gerente gerente) {
		EntityManager manager = factory.createEntityManager();

		GerenteDAO gerenteDao = new GerenteDAO(manager);

		gerenteDao.atualizar(gerente);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// REMOVER GERENTE
	public static void removerGerente(Gerente gerente) throws SQLException {
		EntityManager manager = factory.createEntityManager();

		GerenteDAO gerenteDAO = new GerenteDAO(manager);

		gerenteDAO.remover(gerente);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// LISTAR GERENTE
	public static List<Gerente> listarGerente() {
		EntityManager manager = factory.createEntityManager();

		GerenteDAO gerenteDAO = new GerenteDAO(manager);

		return gerenteDAO.listarTodos();

	}
	

	// ---------------------------------------------------------------------------------------
				// ITEM_CARDAPIO
	// ---------------------------------------------------------------------------------------

	// INSERIR ITEM_CARDAPIO
	public static void inserirItemCardapio(ItemCardapio itemCardapio) {
		EntityManager manager = factory.createEntityManager();

		ItemCardapioDAO itemCardapioDAO = new ItemCardapioDAO(manager);

		itemCardapioDAO.inserir(itemCardapio);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// ATUALIZAR ITEM_CARDAPIO
	public static void atualizarItemCardapio(ItemCardapio itemCardapio) {
		EntityManager manager = factory.createEntityManager();

		ItemCardapioDAO itemCardapioDao = new ItemCardapioDAO(manager);

		itemCardapioDao.atualizar(itemCardapio);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// REMOVER ITEM_CARDAPIO
	public static void removerItemCardapio(ItemCardapio itemCardapio) throws SQLException {
		EntityManager manager = factory.createEntityManager();

		ItemCardapioDAO itemCardapioDAO = new ItemCardapioDAO(manager);

		itemCardapioDAO.remover(itemCardapio);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	
	public static Cardapio buscarCardapio(Long id){
		EntityManager manager = factory.createEntityManager();
		CardapioDAO cardapioDao = new CardapioDAO(manager);
		return cardapioDao.buscarPorId(id);
	}
	
	// LISTAR ITEM_CARDAPIO
	public static List<ItemCardapio> listarItemCardapio() {
		EntityManager manager = factory.createEntityManager();

		ItemCardapioDAO itemCardapioDAO = new ItemCardapioDAO(manager);

		return itemCardapioDAO.listarTodos();

	}

	// ---------------------------------------------------------------------------------------
				// MESA
	// ---------------------------------------------------------------------------------------

	// INSERIR MESA
	public static void inserirMesa(Mesa mesa) {
		EntityManager manager = factory.createEntityManager();

		MesaDAO mesaDAO = new MesaDAO(manager);

		mesaDAO.inserir(mesa);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// ATUALIZAR MESA
	public static void atualizarMesa(Mesa mesa) {
		EntityManager manager = factory.createEntityManager();

		MesaDAO mesaDao = new MesaDAO(manager);

		mesaDao.atualizar(mesa);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}
	
	
	public static List<Mesa> listarMesaPorNome(String nome){
		EntityManager manager = factory.createEntityManager();
		MesaDAO mesaDao = new MesaDAO(manager);
		return mesaDao.listarPorAtributo("nome", nome);
	}
	

	
	public static Mesa buscarMesa(Long id){
		EntityManager manager = factory.createEntityManager();
		MesaDAO mesaDao = new MesaDAO(manager);
		return mesaDao.buscarPorId(id);
	}
	
	
	// REMOVER MESA
	public static void removerMesa(Mesa mesa) throws SQLException {
		EntityManager manager = factory.createEntityManager();

		MesaDAO mesaDAO = new MesaDAO(manager);

		mesaDAO.remover(mesa);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// LISTAR MESA
	public static List<Mesa> listarMesa() {
		EntityManager manager = factory.createEntityManager();

		MesaDAO mesaDAO = new MesaDAO(manager);

		return mesaDAO.listarTodos();

	}
	
	// ---------------------------------------------------------------------------------------
				// PEDIDO
	// ---------------------------------------------------------------------------------------

	// INSERIR PEDIDO
	public static void inserirPedido(Pedido pedido) {
		EntityManager manager = factory.createEntityManager();

		PedidoDAO pedidoDAO = new PedidoDAO(manager);

		pedidoDAO.inserir(pedido);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// ATUALIZAR PEDIDO
	public static void atualizarPedido(Pedido pedido) {
		EntityManager manager = factory.createEntityManager();

		PedidoDAO pedidoDao = new PedidoDAO(manager);

		pedidoDao.atualizar(pedido);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// REMOVER PEDIDO
	public static void removerPedido(Pedido pedido) throws SQLException {
		EntityManager manager = factory.createEntityManager();

		PedidoDAO pedidoDAO = new PedidoDAO(manager);

		pedidoDAO.remover(pedido);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// LISTAR PEDIDO
	public static List<Pedido> listarPedido() {
		EntityManager manager = factory.createEntityManager();

		PedidoDAO pedidoDAO = new PedidoDAO(manager);

		return pedidoDAO.listarTodos();

	}

	// ---------------------------------------------------------------------------------------
				// RESERVA
	// ---------------------------------------------------------------------------------------

	// INSERIR RESERVA
	public static void inserirReserva(Reserva reserva) {
		EntityManager manager = factory.createEntityManager();

		ReservaDAO reservaDAO = new ReservaDAO(manager);

		reservaDAO.inserir(reserva);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// ATUALIZAR RESERVA
	public static void atualizarReserva(Reserva reserva) {
		EntityManager manager = factory.createEntityManager();

		ReservaDAO reservaDao = new ReservaDAO(manager);

		reservaDao.atualizar(reserva);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// REMOVER RESERVA
	public static void removerReserva(Reserva reserva) throws SQLException {
		EntityManager manager = factory.createEntityManager();

		ReservaDAO reservaDAO = new ReservaDAO(manager);

		reservaDAO.remover(reserva);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// LISTAR RESERVA
	public static List<Reserva> listarReserva() {
		EntityManager manager = factory.createEntityManager();

		ReservaDAO reservaDAO = new ReservaDAO(manager);

		return reservaDAO.listarTodos();

	}
	
	// ---------------------------------------------------------------------------------------
				// TRADICIONAL
	// ---------------------------------------------------------------------------------------

	// INSERIR TRADICIONAL
	public static void inserirTradicional(Tradicional tradicional) {
		EntityManager manager = factory.createEntityManager();

		TradicionalDAO tradicionalDAO = new TradicionalDAO(manager);

		tradicionalDAO.inserir(tradicional);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// ATUALIZAR TRADICIONAL
	public static void atualizarTradicional(Tradicional tradicional) {
		EntityManager manager = factory.createEntityManager();

		TradicionalDAO tradicionalDao = new TradicionalDAO(manager);

		tradicionalDao.atualizar(tradicional);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// REMOVER TRADICIONAL
	public static void removerTradicional(Tradicional tradicional) throws SQLException {
		EntityManager manager = factory.createEntityManager();

		TradicionalDAO tradicionalDAO = new TradicionalDAO(manager);

		tradicionalDAO.remover(tradicional);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// LISTAR TRADICIONAL
	public static List<Tradicional> listarTradicional() {
		EntityManager manager = factory.createEntityManager();

		TradicionalDAO tradicionalDAO = new TradicionalDAO(manager);

		return tradicionalDAO.listarTodos();

	}
	
	// ---------------------------------------------------------------------------------------
					// USUARIO
	// ---------------------------------------------------------------------------------------

	// INSERIR USUARIO
	public static void inserirUsuario(Usuario usuario) {
		EntityManager manager = factory.createEntityManager();

		UsuarioDAO usuarioDAO = new UsuarioDAO(manager);

		usuarioDAO.inserir(usuario);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// ATUALIZAR USUARIO
	public static void atualizarUsuario(Usuario usuario) {
		EntityManager manager = factory.createEntityManager();

		UsuarioDAO usuarioDao = new UsuarioDAO(manager);

		usuarioDao.atualizar(usuario);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// REMOVER USUARIO
	public static void removerUsuario(Usuario usuario) throws SQLException {
		EntityManager manager = factory.createEntityManager();

		UsuarioDAO usuarioDAO = new UsuarioDAO(manager);

		usuarioDAO.remover(usuario);
		manager.getTransaction().begin();
		manager.getTransaction().commit();
	}

	// LISTAR USUARIO
	public static List<Usuario> listarUsuario() {
		EntityManager manager = factory.createEntityManager();

		UsuarioDAO usuarioDAO = new UsuarioDAO(manager);

		return usuarioDAO.listarTodos();

	}
	
	public static Usuario autenticar(String login, String senha) {
		
		EntityManager manager = factory.createEntityManager();
		String str = "select u from Usuario u where u.loguin = :login and u.senha = :senha";

		Query q = manager.createQuery(str);
		q.setParameter("login", login);
		q.setParameter("senha", senha);
		List<Usuario> lista = q.getResultList();
		if (lista != null && !lista.isEmpty()) 	{
			return lista.get(0);
		} else {
			return null;
		}
	}
	
	
}
